package com.payment.ktb.activity.main4;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.AdapterItem1Heigth40TextSize15;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.BankType;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/14.
 */

public class BranchBankProvinceActivity extends BaseActivity {

    String timestamp = String.valueOf(new Date().getTime());
    private List<String> listDates;
    AdapterItem1Heigth40TextSize15 adapter;
    @BindView(R.id.lv_branchbankprovince)
    ListView lv_branchbankprovince;
    @BindView(R.id.tv_branchbankprovince_localname)
    TextView tv_branchbankprovince_localname;

    public LocationClient mLocationClient = null;
    public BDLocationListener myListener = new MyLocationListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_branchbankprovince);
        ButterKnife.bind(this);
        initTitle("选择支行");
        //百度地图定位
        mLocationClient = new LocationClient(getApplicationContext());     //声明LocationClient类
        mLocationClient.registerLocationListener( myListener );    //注册监听函数
        initLocation();
        mLocationClient.start();
        initData();
    }


    @OnClick({R.id.rl_city_1_1})
    public void clickEvent(View view) {

        switch (view.getId()) {

            //定位
            case R.id.rl_city_1_1:

                //点击自动定位的城市
                if (TextUtils.isEmpty(tv_branchbankprovince_localname.getText().toString())) {
                    ToastUtils.showToast("定位城市失败！请选择省份");
                } else if ("定位失败".equals(tv_branchbankprovince_localname.getText().toString())) {
                    ToastUtils.showToast("请选择支行所在城市");
                }else {
                    Intent intent = new Intent(mContext,SettleBanknameActivity.class);
                    intent.putExtra("bankType",getIntent().getStringExtra("bankType"));
                    intent.putExtra("bankSimpleName",getIntent().getStringExtra("bankSimpleName"));
                    intent.putExtra("cardNo",getIntent().getStringExtra("cardNo"));
                    intent.putExtra("bankphone",getIntent().getStringExtra("bankphone"));
                    intent.putExtra("city",tv_branchbankprovince_localname.getText().toString());
                    intent.putExtra("httpActionType",getIntent().getStringExtra("httpActionType"));
                    startActivity(intent);
                }


                break;

        }

    }


    private void initData() {

        getProvinces();
        lv_branchbankprovince.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(mContext,BranchBankCityActivity.class);
                intent.putExtra("province",listDates.get(i));
                intent.putExtra("bankType",getIntent().getStringExtra("bankType"));
                intent.putExtra("bankSimpleName",getIntent().getStringExtra("bankSimpleName"));
                intent.putExtra("cardNo",getIntent().getStringExtra("cardNo"));
                intent.putExtra("bankphone",getIntent().getStringExtra("bankphone"));
                intent.putExtra("httpActionType",getIntent().getStringExtra("httpActionType"));
                startActivity(intent);
                finish();
            }
        });
    }

    public void getProvinces() {

        progressDialogUtils.showProgress();

        //构造传输参数
        final Map<String, String> map = new HashMap<String,String>();
        map.put("timestamp",timestamp);

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.CardAreaProvinceSearch, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    //data
                    listDates = new Gson().fromJson(jsonObject.getJSONArray("province").toString(), new TypeToken<List<String>>() {}.getType());
                    //adapter
                    adapter = new AdapterItem1Heigth40TextSize15(mContext,listDates);
                    //bind
                    lv_branchbankprovince.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }





    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            mLocationClient.stop();
            if (location.getLocType() == BDLocation.TypeGpsLocation || location.getLocType() == BDLocation.TypeNetWorkLocation || location.getLocType() == BDLocation.TypeOffLineLocation) {// GPS定位结果
                tv_branchbankprovince_localname.setText(location.getCity());
            } else {
                tv_branchbankprovince_localname.setText("定位失败");
            }
        }

    }



    private void initLocation(){
        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");//可选，默认gcj02，设置返回的定位结果坐标系
        option.setIsNeedAddress(true);//可选，设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//可选，默认false,设置是否使用gps
        option.setLocationNotify(true);//可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        option.setIsNeedLocationPoiList(true);//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        option.setIgnoreKillProcess(false);//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        option.SetIgnoreCacheException(false);//可选，默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//可选，默认false，设置是否需要过滤gps仿真结果，默认需要
        mLocationClient.setLocOption(option);
    }


}
